import { Context, Next } from "koa"

async function errorHandle(ctx: Context, next: Next) {
  try {
    await next()
  } catch (err: any) {
    console.error('server error:', err.message, ctx)
    // console.log(err.message);
    if (err.message == "Authentication Error") {
      ctx.result({
        code: 401,
        message: "invalid token"
      })
      return
    }

    ctx.err({
      message: "server error"
    })
    // ctx.app.emit('error', error, ctx)
  }
}

export default errorHandle